package main

import (
	"bufio"
	"fmt"
	tronWallet "github.com/ranjbar-dev/tron-wallet"
	"github.com/ranjbar-dev/tron-wallet/enums"
	// "math/rand"
	"os"
	"runtime"
	"sync"
	"sync/atomic"
	"time"
	"strings"
)

const letterBytes = "abcdef0123456789"
const (
	letterIdxBits = 6
	letterIdxMask = 1<<letterIdxBits - 1
	letterIdxMax  = 63 / letterIdxBits
)

var count int32
var filePath = "./address.txt"
var isSuccess = false

// func RandStringBytesMask(n int) string {
// 	b := make([]byte, n)
// 	for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; {
// 		if remain == 0 {
// 			cache, remain = rand.Int63(), letterIdxMax
// 		}
// 		if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
// 			b[i] = letterBytes[idx]
// 			i--
// 		}
// 		cache >>= letterIdxBits
// 		remain--
// 	}

// 	return string(b)
// }

func createAddress() (string, string) {
	w := tronWallet.GenerateTronWallet(enums.SHASTA_NODE)
	return w.AddressBase58, w.PrivateKey
}

func writeFile(address string,private string,fileName string) {
	_, err := os.Stat(fileName)
	if err == nil {
		return
	}
	println(address)
	_ = createFile(fileName)
	file, _ := os.OpenFile(fileName, os.O_RDWR|os.O_TRUNC, 0666)
	defer file.Close()
	write := bufio.NewWriter(file)
	_, _ = write.WriteString("公钥：" + address + "\r\n")
	_, _ = write.WriteString("私钥：" + private + "\r\n")
	_, _ = write.WriteString("===========================\r\n")
	_ = write.Flush()
}

func checkAddress() {
	for true {
		address, private := createAddress()
        if address[0:6] == "TRi62f" {
            writeFile(address,private,"./TRi62f.txt")
            break
        }
		if strings.ToLower(address[28:34]) == "r9casc" {
			if strings.ToLower(address[0:6]) == "tri62f" && strings.ToLower(address[28:34]) == "r9casc" {
				writeFile(address,private,"./all/"+address+".txt")
			}
			if address[0:1] == "T" && address[28:34] == "r9CAsC" {
				// 7位
				writeFile(address,private,"./T-r9CAsC.txt")
				if address[0:2] == "TR" && address[28:34] == "r9CAsC" {
					// 8位
					writeFile(address,private,"./TR-r9CAsC.txt")
					if address[0:3] == "TRi" && address[28:34] == "r9CAsC" {
						// 9位
						writeFile(address,private,"./TRi-r9CAsC.txt")
						if address[0:4] == "TRi6" && address[28:34] == "r9CAsC" {
							// 10位
							writeFile(address,private,"./TRi6-r9CAsC.txt")
							if address[0:5] == "TRi62" && address[28:34] == "r9CAsC" {
								// 11位
								writeFile(address,private,"./TRi62-r9CAsC.txt")
								if address[0:6] == "TRi62f" && address[28:34] == "r9CAsC" {
									// 12 位
									writeFile(address,private,"./TRi62f-r9CAsC.txt")
								}
							}
						}
					}
				}
			}
		}
	}
}

func createFile(filePathTemp string) bool {
	_, err := os.Create(filePathTemp)
	if err != nil {
		return false
	} else {
		return true
	}
}

func main() {
	println("数据正在全力运行中，请勿关闭窗口...\r\n")
	_, err := os.Stat(filePath)
	if err != nil {
		if createFile(filePath) == false {
			println("文件创建失败！")
			return
		}
	}
	time.AfterFunc(1*time.Second, func() {
		atomic.AddInt32(&count, -count)
	})

	var (
		wg  sync.WaitGroup
		now = time.Now()
		n   = runtime.NumCPU() * 4
	)

	for i := 0; i < n; i++ {
		wg.Add(1)
		go func() {
			checkAddress()
			wg.Done()
		}()
	}

	wg.Wait()
	fmt.Printf("同时发起 %d 次请求，耗时: %s", n, time.Since(now))
}
